<template>
  <div class="qingjia">
    <el-form ref="ruleForm" :model="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="部门" prop="departmentId">
        <el-select v-model="ruleForm.departmentId" placeholder="部门" @change="changDept">
          <el-option v-for="item in dlist.lists" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

    </el-form>

    <el-table
      :data="leaveTypes"
      style="width: 100%"
    >
      <el-table-column
        label="类型"
        prop="name"
        width="130"
      />
      <el-table-column
        label="是否可用"
      >

        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </template>
      </el-table-column>
    </el-table>
    <div class="buttonDiv">
      <el-button type="primary" @click="submitForm()">保存更新</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
    <div style="margin-top:20px;">
      <el-alert
        type="warning"
        show-icon
        :closable="false"
        class="footer"
      >
        <template>
          <p>事假 请假单位为0.5天 只能提交工作日内的请假单</p>
          <p>当请假开关关闭后，员工则不可在员工端提交请假申请</p>
        </template>
      </el-alert>
    </div>
  </div>
</template>

<script>
import { getLeaveList, saveCfgLeave } from '@/api/attendance'
import { Message } from 'element-ui'
export default {
  inject: ['dlist'],
  data() {
    return {
      ruleForm: {
        departmentId: 0
      },
      leaveTypes: [],
      rules: {
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ]
      },
      names: [
        { name: '年假', id: 60000 },

        { name: '事假', id: 60100 },
        { name: '病假', id: 60200 },
        { name: '婚假', id: 60300 },
        { name: '丧假', id: 60400 },
        { name: '产假', id: 60500 },
        { name: '奖励产假', id: 60600 },
        { name: '陪产假', id: 60700 },
        { name: '探亲假', id: 60800 },
        { name: '工伤假', id: 60900 },
        { name: '调休假', id: 61000 },
        { name: '产检假', id: 61100 },
        { name: '流产假', id: 61200 },
        { name: '长期病假', id: 61300 }
      ]
    }
  },
  watch: {
    leaveTypes(newVal) {
      console.log(newVal)
    }
  },
  async created() {
    this.ruleForm.departmentId = this.dlist.lists[0].id
    this.load()
  },
  methods: {
    async load(id) {
      if (id) {
        this.ruleForm.departmentId = id
      }
      const res = await getLeaveList({ departmentId: this.ruleForm.departmentId })
      this.leaveTypes = res.map(item => {
        const name = this.names.find(item2 => String(item2.id) === item.leaveType)
        item.name = name ? name.name : ''
        return item
      })
      console.log('this.leaveTypes', this.leaveTypes)
    },
    changDept(id) {
      this.ruleForm.departmentId = id
      //   console.log(id)
      this.load()
    },
    closeDialog() {
      this.$emit('closeDialog')
    },
    async submitForm() {
      await saveCfgLeave(this.leaveTypes)
      console.log('this.ruleForm', this.leaveTypes)
      Message.success('保存成功')
      this.closeDialog()
    }
  }
}
</script>

<style>
    .buttonDiv{
        margin-top: 10px;
        text-align: center;
    }
    .footer{
        height: 100px;
    }
</style>
